package org.jsola.hr.helper;

import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.AttendCalendarAddDTO;
import org.jsola.hr.dto.AttendCalendarUpdateDTO;
import org.jsola.hr.entity.AttendCalendarDO;
import org.jsola.hr.query.AttendCalendarQuery;
import org.jsola.hr.service.IAttendCalendarService;
import org.jsola.hr.vo.AttendCalendarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class AttendCalendarTestHelper extends BaseTestHelper {
    
    @Autowired
    private IAttendCalendarService attendCalendarService;

    /**
     * 生成add测试数据
     * @return AttendCalendarAddDTO
     */
    public AttendCalendarAddDTO getAttendCalendarAddDTO(){
        AttendCalendarAddDTO attendCalendarAddDTO = new AttendCalendarAddDTO();
        attendCalendarAddDTO.setAttendPlanId(getRandomValue("varchar",32));
        attendCalendarAddDTO.setYear(getRandomValue("varchar",8));
        attendCalendarAddDTO.setMonth(getRandomValue("varchar",8));
        attendCalendarAddDTO.setDay(getRandomValue("varchar",8));
        attendCalendarAddDTO.setDateType(SafeKit.getInteger(getRandomValue("tinyint",4)));
        attendCalendarAddDTO.setNeedAttend(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        attendCalendarAddDTO.setAttendPeriodJson(getRandomValue("varchar",255));
        attendCalendarAddDTO.setReason(getRandomValue("varchar",255));
        attendCalendarAddDTO.setCompanyId(getRandomValue("varchar",32));
        return attendCalendarAddDTO;
    }


    /**
     * 生成update测试数据
     * @return AttendCalendarUpdateDTO
     */
    public AttendCalendarUpdateDTO getAttendCalendarUpdateDTO(AttendCalendarVO attendCalendarVO){
        return attendCalendarService.selectDOById(attendCalendarVO.getId(), SITE_ID).to(AttendCalendarUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return AttendCalendarQuery
     */
    public AttendCalendarQuery getAttendCalendarQuery() {
        AttendCalendarQuery attendCalendarQuery = new AttendCalendarQuery();
        List<AttendCalendarDO> attendCalendarDOList = attendCalendarService.list(new AttendCalendarDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(attendCalendarDOList)) {
            AttendCalendarDO attendCalendarDO = attendCalendarDOList.get(0);
            attendCalendarQuery = attendCalendarDO.to(AttendCalendarQuery.class);
            if (attendCalendarDO.getGmtCreate() != null) {
                attendCalendarQuery.setGmtCreateStart(DateKit.getBeforeDay(attendCalendarDO.getGmtCreate(),-1));
                attendCalendarQuery.setGmtCreateEnd(DateKit.getBeforeDay(attendCalendarDO.getGmtCreate(),1));
            }
            if (attendCalendarDO.getGmtModified() != null) {
                attendCalendarQuery.setGmtModifiedStart(DateKit.getBeforeDay(attendCalendarDO.getGmtModified(),-1));
                attendCalendarQuery.setGmtModifiedEnd(DateKit.getBeforeDay(attendCalendarDO.getGmtModified(),1));
            }
        }
        return attendCalendarQuery;
    }

    /**
     * 保存示例
     * @return AttendCalendarVO
     */
    public AttendCalendarVO saveAttendCalendarExample(){
        AttendCalendarAddDTO addDTO = this.getAttendCalendarAddDTO();
        AttendCalendarVO attendCalendarVO = attendCalendarService.save(addDTO,buildTokenUser());
        return attendCalendarVO;
    }


}

